package com.cola.web;

import com.alibaba.fastjson.JSON;
import com.cola.popj.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/ajaxServlet")
public class AjaxServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username);
        System.out.println(password);

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        ArrayList<User> arraylistTest = new ArrayList<>();

        User u1 = new User();
        User u2 = new User();

        u1.setUsername(username);
        u1.setPassword(password);
        u2.setUsername(username);
        u2.setPassword(password);


        arraylistTest.add(u1);
        arraylistTest.add(u2);

        String info = JSON.toJSONString(arraylistTest);
        // 打印出檢查
        System.out.println(info);
        // 返回給前端
        out.write(info);
//        User user = new User();
//        user.setUsername("cola");
//        user.setPassword("123");
//        String joinString = JSON.toJSONString(user);
//        response.setContentType("test/json;charset=utf-8");
//        response.getWriter().write(joinString);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
